package com.atguigu.spring5.dao.impl;

import com.atguigu.spring5.dao.IBookDao;
import com.atguigu.spring5.entity.Book;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author : wenchao.long
 * @date : Created in 2020/10/30 8:48 周五
 * @description :
 */
@Repository
@Log
public class BookDaoImpl implements IBookDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void insert(Book book) {
        String sql = "insert into `book` values(?,?,?)";
        int update = jdbcTemplate.update(sql, book.getId(), book.getName(), book.getStatus());
        log.info("影响行数：" + update);
    }

    @Override
    public void update(Book book) {
        String sql = "update `book` set name = ?, status = ? where id = ?";
        int update = jdbcTemplate.update(sql, book.getName(), book.getStatus(), book.getId());
        log.info("影响行数：" + update);
    }

    @Override
    public void delete(Long id) {
        String sql = "delete from `book` where id = ?";
        int update = jdbcTemplate.update(sql, id);
        log.info("影响行数：" + update);
    }

    @Override
    public int count() {
        String sql = "select count(*) from `book`";
        return Optional.ofNullable(jdbcTemplate.queryForObject(sql, Integer.class)).orElse(-1);
    }

    @Override
    public Book getById(Long id) {
        String sql = "select * from `book` where id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Book.class), id);
    }

    @Override
    public List<Book> list() {
        String sql = "select * from `book`";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Book.class));
    }

    @Override
    public void saveBatch(List<Book> books) {
        String sql = "insert into `book` values(?,?,?)";
        List<Object[]> list = new ArrayList<>();
        books.forEach(c -> {
            Object[] objects = new Object[3];
            objects[0] = c.getId();
            objects[1] = c.getName();
            objects[2] = c.getStatus();
            list.add(objects);
        });
        int[] ints = jdbcTemplate.batchUpdate(sql, list);
        log.info("影响行数：" + Arrays.toString(ints));
    }

    @Override
    public void updateBatch(List<Book> books) {
        String sql = "update `book` set name = ? , status = ? where id = ?";
        List<Object[]> list = new ArrayList<>();
        books.forEach(c -> {
            Object[] objects = new Object[3];
            objects[0] = c.getName();
            objects[1] = c.getStatus();
            objects[2] = c.getId();
            list.add(objects);
        });
        int[] ints = jdbcTemplate.batchUpdate(sql, list);
        log.info("影响行数：" + Arrays.toString(ints));
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        String sql = "delete from `book` where id = ?";
        List<Object[]> list = new ArrayList<>();
        ids.forEach(c -> {
            Object[] objects = new Object[1];
            objects[0] = c;
            list.add(objects);
        });
        int[] ints = jdbcTemplate.batchUpdate(sql, list);
        log.info("影响行数：" + Arrays.toString(ints));
    }
}
